//
//  SettingsTableViewController.m
//  NewChat
//
//  Created by 马欢 on 16/2/27.
//  Copyright © 2016年 蓝鸥. All rights reserved.
//

#import "SettingsTableViewController.h"
#import "settingsTableViewCell.h"

@interface SettingsTableViewController ()

@end

@implementation SettingsTableViewController


-(instancetype)initWithStyle:(UITableViewStyle)style{
    self = [super initWithStyle:UITableViewStyleGrouped];
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    初始设置
    self.title = @"设置";
//    注册cell
    UINib *nib = [UINib nibWithNibName:@"settingsTableViewCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"settingsListCell"];
//    背景色
    self.view.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
//cell 设置
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    settingsTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"settingsListCell"];
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = [UIColor colorWithRed:230/255.0 green:200/255.0 blue:50/255.0 alpha:1];
    if (indexPath.section == 0) {
        cell.label.text = @"声音";//声音
        if ([[AVUser currentUser][@"voice"] isEqualToString:@"off"]) {
            cell.MHswitch.on = NO;
        }else{
            cell.MHswitch.on = YES;
        }
        [cell.MHswitch addTarget:self action:@selector(voiceAction:) forControlEvents:(UIControlEventValueChanged)];
    }else if (indexPath.section == 1){
        cell.label.text = @"震动";//震动
        if ([[AVUser currentUser][@"shock"] isEqualToString:@"off"]) {
            cell.MHswitch.on = NO;
        }else{
            cell.MHswitch.on = YES;
        }
        [cell.MHswitch addTarget:self action:@selector(shockAction:) forControlEvents:(UIControlEventValueChanged)];
    }else if (indexPath.section == 2){
        cell.label.text = @"公开主题和回复";//公共主题
        if ([[AVUser currentUser][@"open"] isEqualToString:@"off"]) {
            cell.MHswitch.on = NO;
        }else{
            cell.MHswitch.on = YES;
        }
        [cell.MHswitch addTarget:self action:@selector(openAction:) forControlEvents:(UIControlEventValueChanged)];
    }else if (indexPath.section == 3){
        cell.label.text = @"允许陌生人来电";//允许陌生人来电
        if ([[AVUser currentUser][@"stranger"] isEqualToString:@"off"]) {
            cell.MHswitch.on = NO;
        }else{
            cell.MHswitch.on = YES;
        }
        [cell.MHswitch addTarget:self action:@selector(strangerAction:) forControlEvents:(UIControlEventValueChanged)];
    }
    return cell;
}
//公开主题和回复 switch点击事件
-(void)openAction:(UISwitch *)sender{
    AVUser *user = [AVUser currentUser];
    if (sender.on) {
        [user setObject:@"on" forKey:@"open"];
        [user saveEventually:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                NSLog(@"公开个人主题和回复");
            }
        }];
    }else{
        [user setObject:@"off" forKey:@"open"];
        [user saveEventually:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                NSLog(@"不公开个人主题和回复");
            }
        }];
    }
}
//开启声音 switch点击事件
-(void)voiceAction:(UISwitch *)sender{
    AVUser *user = [AVUser currentUser];
    if (sender.on) {
        [user setObject:@"on" forKey:@"voice"];
        [user saveEventually:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                NSLog(@"声音打开");
            }
        }];
    }else{
        [user setObject:@"off" forKey:@"voice"];
        [user saveEventually:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                NSLog(@"声音关闭");
            }
        }];
    }
}
//开启震动 switch点击事件
-(void)shockAction:(UISwitch *)sender{
    AVUser *user = [AVUser currentUser];
    if (sender.on) {
        [user setObject:@"on" forKey:@"shock"];
        [user saveEventually:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                NSLog(@"震动打开");
            }
        }];
    }else{
        [user setObject:@"off" forKey:@"shock"];
        [user saveEventually:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                NSLog(@"震动关闭");
            }
        }];
    }
}
//允许陌生来电 switch点击事件
-(void)strangerAction:(UISwitch *)sender{
    AVUser *user = [AVUser currentUser];
    if (sender.on) {
        [user setObject:@"on" forKey:@"stranger"];
        [user saveEventually:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                NSLog(@"允许陌生来电");
            }
        }];
    }else{
        [user setObject:@"off" forKey:@"stranger"];
        [user saveEventually:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                NSLog(@"不允许陌生来电");
            }
        }];
    }
}
//view将要出现 tabbar隐藏
-(void)viewWillAppear:(BOOL)animated{
    [self.tabBarController.tabBar setHidden:YES];
}

@end
